package pv3d
{
	import flash.utils.Dictionary;
	
	import org.papervision3d.core.proto.MaterialObject3D;
	
	public class BitmapMaterialAddonManager
	{
		private static var _instance:BitmapMaterialAddonManager
		private var addonList:Dictionary=new Dictionary(true);                //删除指定的动画的时候有用
		private var list:Array=new Array();                                   //以数组的形式存储动画的索引
		public function BitmapMaterialAddonManager()
		{
			if(_instance==null)
			{
				_instance=this;
			}else
			{
				throw new Error("BitmapMaterialAddonManager不能被实例化");
			}
		}
		public static function getInstance():BitmapMaterialAddonManager
		{
			if(_instance==null) return new BitmapMaterialAddonManager;
			return _instance;
		}
		/**
		 * 在材质上添加动画
		 */ 
		public function addAddon(target:MaterialObject3D,id:String,path:String,property:Object):void
		{
			var addon:BitmapMaterialAddon=new BitmapMaterialAddon(id,target,path,property);
			addonList[id]=addon;
			list.push(addon);
		}
		/**
		 * 继续更新指定的材质
		 */ 
		public function startAddon(id:String):Boolean
		{
			if(addonList[id]==null)return false;
			var temp:BitmapMaterialAddon=addonList[id];
			temp.start();
			return true;
		}
		/**
		 * 继续更新指定的材质
		 */ 
		public function stopAddon(id:String):Boolean
		{
			if(addonList[id]==null)return false;
			var temp:BitmapMaterialAddon=addonList[id];
			temp.stop();
			return true;
		}
		/**
		 * 继续更新所有的动画
		 */ 
		public function startAll():void
		{
			for each(var addon:BitmapMaterialAddon in list)
			{
				addon.start();
			}
		}
		/**
		 * 停止更新所有的动画
		 */ 
		public function stopAll():void
		{
			for each(var addon:BitmapMaterialAddon in list)
			{
				addon.stop();
			}
		}
		/**
		 * 删除指定的材质动画
		 */ 
		public function removeAddon(id:String):Boolean
		{
			if(id!=null && addonList[id]!=null)
			{
				var temp:BitmapMaterialAddon=addonList[id];
				temp.dispose();
				temp=null;
				addonList[id]=null;
				return true;
			}
			return false;
		}
		/**
		 * 删除所有的材质动画
		 */ 
		public function removeAllAddon():void
		{
			for each(var addon:BitmapMaterialAddon in list)
			{
				addonList[addon.id]=null;
				addon.dispose();
				addon=null;
			}
			list=new Array();
		}
	}
}